package phoenics;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

import java.util.HashMap;
import java.util.Map;

/**
 * @author phoenics@126.com
 * @create 2018-09-03 16:55
 **/
public class Talipay2 {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Talipay2.class);
    public static void main(String[] args) throws AlipayApiException {
        //ScheckSignAndDecrypt();

        //String biz_content = "<XML><AppId><![CDATA[2018081861065904]]></AppId><FromUserId><![CDATA[2088102122485786]]></FromUserId><CreateTime>1377228401913</CreateTime><MsgType><![CDATA[click]]></MsgType><EventType><![CDATA[event]]></EventType><ActionParam><![CDATA[authentication]]></ActionParam><AgreementId><![CDATA[201308220000000994]]></AgreementId><AccountNo><![CDATA[null]]></AccountNo><UserInfo><![CDATA[{\"logon_id\":\"15858179811\",\"user_name\":\"许旦辉\"}]]></UserInfo></XML>";
        String biz_content = "a=123";
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuVPTRGXlspPGaVPvhddhORSeNINE+ZSk+K4EER1Yk4vKDndl2D167rnaUbisVoUkOH8R1CH0NgHBzUfBX5Nc75C0UmExUGk5C6uvLe0+r+2NPu7/iZFPzCfCyZgCZ0Yyoyx/4ErwY2RKbP1fKWR/Q28SrJgLopfw0RGbLU5a/YwHyyco/FFhShCVSySHjOsMFqFR9je3tR2zjakBxImpJGo4p8cZK97BKOydhCO6oI2cuvbSfaSHFN02fvm8cPkpihOLT4iBPzJiP4SklQQgnyQGEJUsV943sTyAqOaFIeDNaHUHttMPifPngnWw4Xc7HMzbBhOZ1ofLSNJv1FxhCQIDAQAB";
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC5U9NEZeWyk8ZpU++F12E5FJ40g0T5lKT4rgQRHViTi8oOd2XYPXruudpRuKxWhSQ4fxHUIfQ2AcHNR8Ffk1zvkLRSYTFQaTkLq68t7T6v7Y0+7v+JkU/MJ8LJmAJn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xJnFu7HerutECgYEAv286yqjRj4E9OXvPsPeYoxtjwVWjMcMgRetk54naR98UXqlPd/uk4BMUbOQF1bm+viI91gSgtWdEPvlAhJJfZmBVNQVJVy4JmNDRxdhY77eKL0Gw20jco5nxmkILcn9kHBLbjmZeEP06hJR2h2YsN9XioUHF6oZXj2x16K13geECgYAQlomrtU4iZml+Xw76laCBaIaIt5etnOR/iyhbBWMJgQlwkPrejm+6wNWyqS/E3JN+/sZD2u2G/Mh5Ne92oIL6iOHmBuksbj+MI00wjsRjhPuH/4hFf2Fqo6WOMpLkkb0z/9D8r2uxqeEW5lzyKZWO33Dxb/Rpr++jC6BfliQNiA==";
        Map<String, String> params = new HashMap<>();
        params.put("biz_content", AlipaySignature.rsaEncrypt(biz_content, publicKey, "UTF-8"));
        params.put("charset", "GBK");
        params.put("service", "alipay.mobile.public.message.notify");
        params.put("sign_type", "RSA2");
        String vv=AlipaySignature.rsaSign(params, privateKey, "GBK");
        System.out.println(vv);
        params.put("sign", vv);

        // 验签&解密
        String resultContent = AlipaySignature.checkSignAndDecrypt(params, publicKey, privateKey,
                true, true);

        System.out.println(resultContent);



    }

    private static void checkSignAndDecrypt() throws AlipayApiException {
        // 参数构建
        String biz_content = "<XML><AppId><![CDATA[2013082200024893]]></AppId><FromUserId><![CDATA[2088102122485786]]></FromUserId><CreateTime>1377228401913</CreateTime><MsgType><![CDATA[click]]></MsgType><EventType><![CDATA[event]]></EventType><ActionParam><![CDATA[authentication]]></ActionParam><AgreementId><![CDATA[201308220000000994]]></AgreementId><AccountNo><![CDATA[null]]></AccountNo><UserInfo><![CDATA[{\"logon_id\":\"15858179811\",\"user_name\":\"许旦辉\"}]]></UserInfo></XML>";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB";
        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMiAec6fsssguUoRN3oEVEnQaqBLZjeafXAxCbKH3MTJaXPmnXOtqFFqFtcB8J9KqyFI1+o6YBDNIdFWMKqOwDDWPKqtdo90oGav3QMikjGYjIpe/gYYCQ/In/oVMVj326GmKrSpp0P+5LNCx59ajRpO8//rnOLd6h/tNxnfahanAgMBAAECgYEAusouMFfJGsIWvLEDbPIhkE7RNxpnVP/hQqb8sM0v2EkHrAk5wG4VNBvQwWe2QsAuY6jYNgdCPgTNL5fLaOnqkyy8IobrddtT/t3vDX96NNjHP4xfhnMbpGjkKZuljWKduK2FAh83eegrSH48TuWS87LjeZNHhr5x4C0KHeBTYekCQQD5cyrFuKua6GNG0dTj5gA67R9jcmtcDWgSsuIXS0lzUeGxZC4y/y/76l6S7jBYuGkz/x2mJaZ/b3MxxcGQ01YNAkEAzcRGLTXgTMg33UOR13oqXiV9cQbraHR/aPmS8kZxkJNYows3K3umNVjLhFGusstmLIY2pIpPNUOho1YYatPGgwJBANq8vnj64p/Hv6ZOQZxGB1WksK2Hm9TwfJ5I9jDu982Ds6DV9B0L4IvKjHvTGdnye234+4rB4SpGFIFEo+PXLdECQBiOPMW2cT8YgboxDx2E4bt8g9zSM5Oym2Xeqs+o4nKbcu96LipNRkeFgjwXN1708QuNNMYsD0nO+WIxqxZMkZsCQHtS+Jj/LCnQZgLKxXZAllxqSTlBln2YnBgk6HqHLp8Eknx2rUXhoxE1vD9tNmom6PiaZlQyukrQkp5GOMWDMkU=";
        Map<String, String> params = new HashMap<String, String>();
        params.put("biz_content", AlipaySignature.rsaEncrypt(biz_content, publicKey, "UTF-8"));
        params.put("charset", "UTF-8");
        params.put("service", "alipay.mobile.public.message.notify");
        params.put("sign_type", "RSA");
        params.put("sign", AlipaySignature.rsaSign(params, privateKey, "UTF-8"));

        // 验签&解密
        String resultContent = AlipaySignature.checkSignAndDecrypt(params, publicKey, privateKey,
                true, true);

        System.out.println(resultContent);
    }
}
